package com.citywithincity.db;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DbUtil {
	
	
	private static Map<Class<?>, Map<String, String>> classMap = new HashMap<Class<?>, Map<String,String>>();
	
	
	/**
	 * build
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	public static List<Map<String,Object>> buildRsList(ResultSet rs) throws SQLException{
		ResultSetMetaData data = rs.getMetaData();
		int count = data.getColumnCount();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		while(rs.next()) {
			Map<String, Object> map = new HashMap<String, Object>();
			for (int i = 1; i <= count; ++i) {
				String label = data.getColumnLabel(i);
				map.put(label, rs.getObject(i));
			}
			list.add(map);
		}
		return list;
	}
	
	
	public static void close(ResultSet rs) {
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				throw new DBException(e);
			}
		}
		
	}
	
	public static void close(ResultSet rs, PreparedStatement ps) {
		SQLException ex = null;
		if (rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				ex = e;
			}
		}
		if (ps != null){
			try {
				ps.close();
			} catch (SQLException e) {
				ex = e;
			}
		}
		
		if (ex != null) {
			throw new RuntimeException(ex);
		}
	}
	public static void close(PreparedStatement ps) {
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				throw new DBException(e);
			}
		}

	}
	public static void setPs(PreparedStatement ps,Object[] values) throws SQLException {
		int index = 1;
		for (Object object : values) {
			ps.setObject(index++, object);
		}
	}
	
	public static byte[] getBlob(Blob blob){
		if (blob != null) {
			InputStream is = null;
			try {
				is = blob.getBinaryStream();
				byte[] data = new byte[(int) blob.length()];
				is.read(data);
				is.close();
				return data;
			} catch (Exception e) {
				throw new RuntimeException(e);
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}
		return null;
	}
	
	public static String getClob(Clob clob){
		if(clob==null)return "";
		Reader reader = null;
		try{
			reader = clob.getCharacterStream();
			char[] buffer = new char[(int) clob.length()];
			reader.read(buffer);
			return new String(buffer);
		} catch (SQLException e) {
			throw new DBException(e);
		} catch (Exception e) {
			throw new DBException(e);
		} finally{
			try {
				reader.close();
			} catch (IOException e) {
				
			}
		}
		
	}


	public static void close(Connection connection) {
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				
			}
		}
	}
	
}
